#include"asm_utils.h"
#include"interrupt.h"
#include"stdio.h"
#include"program.h"
#include"thread.h"

//屏幕IO处理器
STDIO stdio;
//中断管理器
InterruptManager interruptManager;
//程序管理器
ProgramManager programManager;

void start_thread(void* arg);
void second_thread(void* arg);
void third_thread(void* arg);

void start_thread(void* arg)
{
	//第一个线程不可以返回
	printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
	if(!programManager.running->pid)
	{
		programManager.executeThread(second_thread,nullptr,"second thread",6);
		programManager.executeThread(second_thread,nullptr,"third thread",3);
		programManager.executeThread(second_thread,nullptr,"forth thread",3);
		programManager.executeThread(second_thread,nullptr,"fifth thread",0);
		programManager.executeThread(third_thread,nullptr,"sixth thread",7);
	}
	// asm_halt();
}
void second_thread(void* arg)
{
	// while(true)
	{
		printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
	}
}
void third_thread(void* arg)
{
	int i=0;
	while(i<100000000)
	{
		i++;
	}
	printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
}

extern "C" void setup_kernel()
{
	interruptManager.initialize();//中断处理部件
	interruptManager.enableTimeInterrupt();
	interruptManager.setTimeInterrupt((void*)asm_time_interrupt_handler);

	stdio.initialize();//屏幕IO处理部件

	programManager.initialize();//程序管理器
	int pid=programManager.executeThread(start_thread,nullptr,"first thread",7);
	if(pid==-1)
	{
		printf("can not execute thread!");
		asm_halt();
	}
	// ListItem* item=programManager.readyPrograms.front();
	// PCB* firstThread=ListItemToPCB(item,tagInGeneralList);
	// firstThread->status=RUNNING;
	// programManager.readyPrograms.pop_front();
	// programManager.running=firstThread;
	// asm_switch_thread(0,firstThread);
	// programManager.schedule_first_in_first_out();
	// programManager.schedule_multilevel_feedback_queue();
	asm_halt();
}
